﻿using System;
using Teigha.Core;
using Teigha.TD;

namespace OdaOperation.Core.Simplifier
{
    internal class Dumper
    {
        private OdDbDatabase m_dataBase;

        public Dumper(OdDbDatabase database)
        {
            m_dataBase = database;
        }

        internal void Traverse(DrawCallback func, DrawCallback endFun)
        {
            MemoryManager memoryManager = MemoryManager.GetMemoryManager();
            MemoryTransaction value = memoryManager.StartTransaction();
            try
            {
                OdGiContextForDbDatabase odGiContextForDbDatabase = OdGiContextForDbDatabase.createObject();
                odGiContextForDbDatabase.enableGsModel(enable: true);
                odGiContextForDbDatabase.setDatabase(m_dataBase);
                SimpliferVectorizeDevice simpliferVectorizeDevice = new SimpliferVectorizeDevice();
                DunmperVectorizer dunmperVectorizer = new DunmperVectorizer();
                SimplifierVectorizeView view = new SimplifierVectorizeView();
                SimplifierGeometry geometrySimplifier = new SimplifierGeometry();
                dunmperVectorizer.SetDrawCallback(func.Invoke);
                dunmperVectorizer.SetDrawEndCallback(endFun.Invoke);
                simpliferVectorizeDevice.SetGeometrySimplifier(geometrySimplifier);
                simpliferVectorizeDevice.SetView(view, dunmperVectorizer);
                OdGsModelLayoutHelper odGsModelLayoutHelper = OdGsModelLayoutHelper.cast(TD_Db.setupActiveLayoutViews(simpliferVectorizeDevice, odGiContextForDbDatabase));
                uint[] lightPalette = AllPalettes.getLightPalette();
                odGsModelLayoutHelper.setBackgroundColor(lightPalette[0]);
                odGsModelLayoutHelper.setLogicalPalette(lightPalette, 256);
                OdGsView.RenderMode mode = OdGsView.RenderMode.kFlatShaded;
                odGsModelLayoutHelper.activeView().setMode(mode);
                OdDbBlockTableRecord odDbBlockTableRecord = m_dataBase.getModelSpaceId().safeOpenObject() as OdDbBlockTableRecord;
                OdGeExtents3d odGeExtents3d = new OdGeExtents3d();
                odDbBlockTableRecord.getGeomExtents(odGeExtents3d);
                odGsModelLayoutHelper.activeView().zoomExtents(odGeExtents3d.minPoint(), odGeExtents3d.maxPoint());
                double num = Math.Abs(odGeExtents3d.maxPoint().x - odGeExtents3d.minPoint().x);
                double num2 = Math.Abs(odGeExtents3d.maxPoint().y - odGeExtents3d.minPoint().y);
                double zz = odGeExtents3d.minPoint().distanceTo(odGeExtents3d.maxPoint());
                OdGePoint3d target = odGeExtents3d.center();
                OdGePoint3d position = odGeExtents3d.center() + new OdGeVector3d(0.0, 0.0, zz);
                OdGeVector3d upVector = new OdGeVector3d(0.0, 1.0, 0.0);
                odGsModelLayoutHelper.activeView().setView(position, target, upVector, num * 1.1, num2 * 1.1, OdGsView.Projection.kParallel);
                OdGsDCRect outputRect = new OdGsDCRect(new OdGsDCPoint(0, 0), new OdGsDCPoint(Math.Max((int)(num * 1.1 + 0.5), 10), Math.Max((int)(num2 * 1.1 + 0.5), 10)));
                simpliferVectorizeDevice.onSize(outputRect);
                odGsModelLayoutHelper.update();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                memoryManager.StopTransaction(value);
            }
        }
    }
}